package com.genersoft.iot.vmp.vmanager.gb28181.play;



import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lenovo
 */
@RestController
public class VideoController {

    @GetMapping("/api/getVideo/{deviceId}/{channelId}/{date}")
    public ResponseResult getVideo(@PathVariable("deviceId") String deviceId,
                                   @PathVariable("channelId") String channelId,
                                   @PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {

        String url = String.format("http://8.130.78.223:6080/record/rtp/%s_%s/%s", deviceId, channelId, date);


       List list = new ArrayList();
        try {
            Document doc = Jsoup.connect(url).get();
            Elements links = doc.select("a[href]");

            // 使用正则表达式匹配并移除文件大小
            String regex = "\\(.*\\)";
            Pattern pattern = Pattern.compile(regex);

            for (Element link : links) {

                String outputString = link.text().replaceAll(regex, "");
                if(outputString.equals("根目录") || outputString.equals("上级目录")){
                    continue;
                }
                String newUrl=url+"/"+outputString;
                list.add(newUrl);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ResponseResult<>(200,"录像返回成功",list);

    }

}
